﻿Imports WoWArmoryParser
Imports System.IO


Public Class Form1


    'Written by C Lacey
    'Created using Visual Studio 2008
    'Utilizing the WoW Armory Parser
    'Released under the Microsoft Reciprocal License (Ms-RL)

    'Character Variables
    Dim c_name As String    'Character name
    Dim c_race As Integer   'Character race
    Dim c_class As Integer  'Character class
    Dim faction As Integer  'Facterion
    Dim health As Integer   'Health value
    Dim spell As Integer    'Mana value
    Dim guild As String     'Guild name
    Dim gender As Integer   'Character gender (male or female)
    Dim realm As Integer    'Realm (server)
    Dim reg_click As Integer    'Saves the value of comboRegion
    Dim char_realm As String    'Saves the value of either ComboEU or comboUSA
    Dim char_sheet As String    'Contains the URL to allow the user to view the Blizzard weaponry page
    Dim load_sheet As Boolean   'Sets whether a Viewer Character File has been loaded

    'Weapon Variables
    Dim weap_name(20) As String 'Sets the Weaponary Name
    Dim weap_icon(20) As String 'Sets blizzard icon used for weaonary
    Dim weap_id(20) As Integer  'Set the weapon id used by blizzard
    ' *Denotes obsolete - will be removed ASAP


    'File system Variables
    Dim file_char_name As String    'Varable used for loading and saving character information
    Dim file_char_region As String    'Varable used for loading and saving character information
    Dim file_char_realm As String    'Varable used for loading and saving character information
    Dim regionstr As String    'Varable used for loading and saving character information

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        clear()
        Me.Show()
        Me.Width = 859
        Me.Height = 593
        Me.Top = 201
        Me.Left = 210
        comboRegion.SelectedIndex = 0
        If comboRegion.SelectedIndex = 0 Then
            comboEU.Visible = True
            comboUSA.Visible = False
            comboEU.SelectedIndex = 0
        ElseIf comboRegion.SelectedIndex = 1 Then
            comboEU.Visible = False
            comboUSA.Visible = True
            comboUSA.SelectedIndex = 0
        End If

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles comboRegion.SelectedIndexChanged
        If comboRegion.SelectedIndex = 0 Then
            comboEU.Visible = True
            comboUSA.Visible = False
            comboEU.SelectedIndex = 0
        ElseIf comboRegion.SelectedIndex = 1 Then
            comboEU.Visible = False
            comboUSA.Visible = True
            comboUSA.SelectedIndex = 0
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Trim(txtName.Text) = "" Then
            clear()
            txtName.Focus()
            Exit Sub
        End If
        txtName.Text = StrConv(txtName.Text, VbStrConv.ProperCase)
        file_char_name = Trim(txtName.Text)
        comboRegion.Enabled = False
        If comboRegion.SelectedIndex = 0 Then
            comboEU.Visible = True
            char_realm = Trim(comboEU.SelectedItem)
            reg_click = 0
            comboEU.Enabled = False
        ElseIf comboRegion.SelectedIndex = 1 Then
            comboUSA.Visible = True
            char_realm = Trim(comboUSA.SelectedItem)
            reg_click = 1
            comboUSA.Enabled = False
        End If
        SaveFileToolStripMenuItem.Enabled = True
        get_character_information()
    End Sub

    Private Sub get_character_information()

        Dim wow_icon As String
        Dim class_info As String
        Dim class_icon As String
        CheckBox1.Enabled = True
        Try
            Dim character As CharacterInfo = ArmoryParser.GetCharacter(reg_click, Trim(char_realm), Trim(file_char_name))
            If load_sheet = True Then
                txtName.Text = file_char_name
            End If
            set_armory(reg_click, Trim(char_realm), Trim(txtName.Text))
            create_html(Trim(char_realm), Trim(txtName.Text), reg_click)
            comboArmour.Enabled = True
            c_name = Trim(txtName.Text)
            c_race = character.Race
            c_class = character.Race
            Me.Text = Trim(txtName.Text) & "@" & comboEU.SelectedItem & " Armory Viewer."
            If character.Faction = FactionEnum.Alliance Then
                picHorde.Visible = False
                picAlliance.Visible = True
            ElseIf character.Faction = FactionEnum.Horde Then
                picHorde.Visible = True
                picAlliance.Visible = False
            End If
            If character.CharacterClass = 11 Then
                class_info = "druid.jpg"
            ElseIf character.CharacterClass = 3 Then
                class_info = "hunter.jpg"
            ElseIf character.CharacterClass = 8 Then
                class_info = "mage.jpg"
            ElseIf character.CharacterClass = 2 Then
                class_info = "paladin.jpg"
            ElseIf character.CharacterClass = 5 Then
                class_info = "priest.jpg"
            ElseIf character.CharacterClass = 4 Then
                class_info = "rouge.jpg"
            ElseIf character.CharacterClass = 7 Then
                class_info = "shaman.jpg"
            ElseIf character.CharacterClass = 9 Then
                class_info = "warlock.jpg"
            ElseIf character.CharacterClass = 1 Then
                class_info = "warrior.jpg"
            End If
            char_sheet = character.ArmoryWebPath.AbsoluteUri
            class_icon = "http://eu.wowarmory.com/_images/icons/class/" & character.CharacterClass & ".gif"
            wow_icon = "http://eu.wowarmory.com/_images/portraits/wow-default/" & character.Gender & "-" & character.Race & "-" & character.CharacterClass & ".gif"
            If character.Level >= 70 And character.Level <= 79 Then
                wow_icon = "http://eu.wowarmory.com/_images/portraits/wow-70/" & character.Gender & "-" & character.Race & "-" & character.CharacterClass & ".gif"
            ElseIf character.Level >= 80 Then
                wow_icon = "http://eu.wowarmory.com/_images/portraits/wow-80/" & character.Gender & "-" & character.Race & "-" & character.CharacterClass & ".gif"
            End If
            picClass.ImageLocation = class_icon
            picRace.ImageLocation = wow_icon
            Label5.Text = character.Race.ToString
            Label4.Text = character.CharacterClass.ToString
            Application.DoEvents()
            lblHealth.Text = "Health:" & Str(character.HealthBar.Total)
            lblSpell.Text = "Mana:" & Str(character.SecondBar.Total)
            lblStrength.Text = "Strength:" & Str(character.BaseStats.Strength)
            lblAgility.Text = "Agility:" & Str(character.BaseStats.Agility)
            lblStamina.Text = "Stamina:" & Str(character.BaseStats.Stamina)
            lblIntellect.Text = "Intellect:" & Str(character.BaseStats.Intellect)
            lblSpirit.Text = "Spirit:" & Str(character.BaseStats.Spirit)
            lblArmor.Text = "Armor:" & Str(character.BaseStats.Armor)
            lblLevel.Text = "Level:" & character.Level
            ProgressBar1.Value = Val(character.Level)
            lblGuild.Text = "Guild: " & character.GuildName
            ProgressBar1.Value = character.AchievementPoints
            SaveFileToolStripMenuItem.Enabled = True
            Application.DoEvents()
        Catch
        End Try
    End Sub

    Private Sub set_armory(ByVal server As Integer, ByVal realm As String, ByVal ch_name As String)
        Try
            Dim character As CharacterInfo = ArmoryParser.GetCharacter(server, Trim(realm), Trim(ch_name))
            Dim x As Integer
            'Set Head
            Try
                weap_name(0) = character.EquippedItems.Slots.Head.Name
                weap_icon(0) = "http://eu.wowarmory.com/wow-icons/_images/64x64/" + character.EquippedItems.Slots.Head.Icon + ".jpg"
                weap_id(0) = character.EquippedItems.Slots.Head.ID
            Catch
            End Try
            'Set necklace
            Try
                weap_name(1) = character.EquippedItems.Slots.Necklace.Name
                weap_icon(1) = "http://eu.wowarmory.com/wow-icons/_images/64x64/" + character.EquippedItems.Slots.Necklace.Icon + ".jpg"
                weap_id(1) = character.EquippedItems.Slots.Necklace.ID
            Catch
            End Try
            'Set shoulders
            Try
                weap_name(2) = character.EquippedItems.Slots.Shoulders.Name
                weap_icon(2) = "http://eu.wowarmory.com/wow-icons/_images/64x64/" + character.EquippedItems.Slots.Shoulders.Icon + ".jpg"
                weap_id(2) = character.EquippedItems.Slots.Shoulders.ID
            Catch
            End Try
            'Set back
            Try
                weap_name(3) = character.EquippedItems.Slots.Back.Name
                weap_icon(3) = "http://eu.wowarmory.com/wow-icons/_images/64x64/" + character.EquippedItems.Slots.Back.Icon + ".jpg"
                weap_id(3) = character.EquippedItems.Slots.Back.ID
            Catch
            End Try
            'Set chest
            Try
                weap_name(4) = character.EquippedItems.Slots.Chest.Name
                weap_icon(4) = "http://eu.wowarmory.com/wow-icons/_images/64x64/" + character.EquippedItems.Slots.Chest.Icon + ".jpg"
                weap_id(4) = character.EquippedItems.Slots.Chest.ID
            Catch
            End Try
            'Set shirt
            Try
                weap_name(5) = character.EquippedItems.Slots.Shirt.Name
                weap_icon(5) = "http://eu.wowarmory.com/wow-icons/_images/64x64/" + character.EquippedItems.Slots.Shirt.Icon + ".jpg"
                weap_id(5) = character.EquippedItems.Slots.Shirt.ID
            Catch
            End Try
            'Set tabard
            Try
                weap_name(6) = character.EquippedItems.Slots.Tabard.Name
                weap_icon(6) = "http://eu.wowarmory.com/wow-icons/_images/64x64/" + character.EquippedItems.Slots.Tabard.Icon + ".jpg"
                weap_id(6) = character.EquippedItems.Slots.Tabard.ID
            Catch
            End Try
            'Set wrist
            Try
                weap_name(7) = character.EquippedItems.Slots.Wrists.Name
                weap_icon(7) = "http://eu.wowarmory.com/wow-icons/_images/64x64/" + character.EquippedItems.Slots.Wrists.Icon + ".jpg"
                weap_id(7) = character.EquippedItems.Slots.Wrists.ID
            Catch
            End Try
            'Set hands
            Try
                weap_name(8) = character.EquippedItems.Slots.Hands.Name
                weap_icon(8) = "http://eu.wowarmory.com/wow-icons/_images/64x64/" + character.EquippedItems.Slots.Hands.Icon + ".jpg"
                weap_id(8) = character.EquippedItems.Slots.Hands.ID
            Catch
            End Try
            'Set waist
            Try
                weap_name(9) = character.EquippedItems.Slots.Waist.Name
                weap_icon(9) = "http://eu.wowarmory.com/wow-icons/_images/64x64/" + character.EquippedItems.Slots.Waist.Icon + ".jpg"
                weap_id(9) = character.EquippedItems.Slots.Waist.ID
            Catch
            End Try
            'Set legs
            Try
                weap_name(10) = character.EquippedItems.Slots.Legs.Name
                weap_icon(10) = "http://eu.wowarmory.com/wow-icons/_images/64x64/" + character.EquippedItems.Slots.Legs.Icon + ".jpg"
                weap_id(10) = character.EquippedItems.Slots.Legs.ID
            Catch
            End Try
            'Set feet
            Try
                weap_name(11) = character.EquippedItems.Slots.Feet.Name
                weap_icon(11) = "http://eu.wowarmory.com/wow-icons/_images/64x64/" + character.EquippedItems.Slots.Feet.Icon + ".jpg"
                weap_id(11) = character.EquippedItems.Slots.Feet.ID
            Catch
            End Try
            'Set ring1
            Try
                weap_name(12) = character.EquippedItems.Slots.Ring1.Name
                weap_icon(12) = "http://eu.wowarmory.com/wow-icons/_images/64x64/" + character.EquippedItems.Slots.Ring1.Icon + ".jpg"
                weap_id(12) = character.EquippedItems.Slots.Ring1.ID
            Catch
            End Try
            'Set wrist
            Try
                weap_name(13) = character.EquippedItems.Slots.Ring2.Name
                weap_icon(13) = "http://eu.wowarmory.com/wow-icons/_images/64x64/" + character.EquippedItems.Slots.Ring2.Icon + ".jpg"
                weap_id(13) = character.EquippedItems.Slots.Ring2.ID
            Catch
            End Try
            'Set trinket1
            Try
                weap_name(14) = character.EquippedItems.Slots.Trinket1.Name
                weap_icon(14) = "http://eu.wowarmory.com/wow-icons/_images/64x64/" + character.EquippedItems.Slots.Trinket1.Icon + ".jpg"
                weap_id(14) = character.EquippedItems.Slots.Trinket1.ID
            Catch
            End Try
            'Set trinket2
            Try
                weap_name(15) = character.EquippedItems.Slots.Trinket2.Name
                weap_icon(15) = "http://eu.wowarmory.com/wow-icons/_images/64x64/" + character.EquippedItems.Slots.Trinket2.Icon + ".jpg"
                weap_id(15) = character.EquippedItems.Slots.Trinket2.ID
            Catch
            End Try
            'Set mainhand
            Try
                weap_name(16) = character.EquippedItems.Slots.MainHand.Name
                weap_icon(16) = "http://eu.wowarmory.com/wow-icons/_images/64x64/" + character.EquippedItems.Slots.MainHand.Icon + ".jpg"
                weap_id(16) = character.EquippedItems.Slots.MainHand.ID
            Catch
            End Try
            'Set offhand
            Try
                weap_name(17) = character.EquippedItems.Slots.OffHand.Name
                weap_icon(17) = "http://eu.wowarmory.com/wow-icons/_images/64x64/" + character.EquippedItems.Slots.OffHand.Icon + ".jpg"
                weap_id(17) = character.EquippedItems.Slots.OffHand.ID
            Catch
            End Try
            'Set ranged
            Try
                weap_name(18) = character.EquippedItems.Slots.Ranged.Name
                weap_icon(18) = "http://eu.wowarmory.com/wow-icons/_images/64x64/" + character.EquippedItems.Slots.Ranged.Icon + ".jpg"
                weap_id(18) = character.EquippedItems.Slots.Ranged.ID
            Catch
            End Try
        Catch
        End Try
        For x = 0 To 18
            comboArmour.Items.Add(weap_name(x))
        Next
        comboArmour.SelectedIndex = 0
    End Sub

    Sub clear()
        Dim x As Integer
        picClass.ImageLocation = "http://i40.tinypic.com/vhbz43.jpg"
        picRace.ImageLocation = "http://i40.tinypic.com/vhbz43.jpg"
        picArmor.ImageLocation = "http://i40.tinypic.com/vhbz43.jpg"
        lblHealth.Text = "Health:"
        lblSpell.Text = "Mana:"
        lblGuild.Text = "Guild:"
        lblLevel.Text = "Level:"
        txtName.Text = ""
        ProgressBar1.Value = 0
        lblStrength.Text = "Strength:"
        lblAgility.Text = "Agility:"
        lblStamina.Text = "Stamina:"
        lblIntellect.Text = "Intellect:"
        lblSpirit.Text = "Spirit:"
        lblArmor.Text = "Armor:"
        Label4.Text = ""
        Label5.Text = ""
        Me.Text = "Armory Viewer."
        comboRegion.SelectedIndex = 0
        comboEU.SelectedIndex = 0
        comboUSA.SelectedIndex = 0
        comboArmour.Enabled = False
        txtName.Focus()
        picHorde.Visible = False
        picAlliance.Visible = False
        WebBrowser2.Navigate("")
        SaveFileToolStripMenuItem.Enabled = False
        Me.Width = 859
        Me.Height = 593
        Me.Top = 201
        Me.Left = 210
        CheckBox1.Checked = False
        CheckBox1.Enabled = False
        If reg_click = 0 Then
            comboEU.Enabled = True
            comboRegion.Enabled = True
        ElseIf reg_click = 1 Then
            comboUSA.Enabled = True
            comboRegion.Enabled = True
        End If
        For x = 0 To 20
            weap_name(x) = ""
            weap_icon(x) = ""
            weap_id(x) = 0
            comboArmour.Items.Clear()
        Next
    End Sub

    Private Sub comboArmour_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comboArmour.SelectedIndexChanged
        Dim c As Integer
        c = comboArmour.SelectedIndex
        picArmor.ImageLocation = weap_icon(c)
        WebBrowser2.Navigate("http://wow.allakhazam.com/ihtml?" & weap_id(c))
    End Sub

    Sub create_html(ByVal realm As String, ByVal ch_name As String, ByVal server As Integer)
        Dim data(12) As String
        Dim datafile As StreamWriter = New StreamWriter(Application.StartupPath & "\character-create.html")
        Dim count As Short

        If server = RegionEnum.USA Then
            regionstr = "http://www.wowarmory.com/character-model-embed.xml?r=" & Trim(realm) & "&cn=" & Trim(ch_name) & "&rhtml=true"
        ElseIf server = RegionEnum.Europe Then
            regionstr = "http://eu.wowarmory.com/character-model-embed.xml?r=" & Trim(realm) & "&cn=" & Trim(ch_name) & "&rhtml=true"
        End If
        data(0) = "<html>"
        data(1) = "<head>"
        data(2) = "<title></title>"
        data(3) = "<meta http-equiv=""Content-Type"" content=""text/html; charset=iso-8859-1"""
        data(4) = "</head>"
        data(5) = "<body bgcolor=""Black"">"
        data(6) = "<iframe src=""" & regionstr & "scrolling=""no"" height=""588"" width=""321"" frameborder=""0""></iframe>"
        data(7) = "</body>"
        data(8) = "</html>"
        For count = 0 To 8
            datafile.WriteLine(data(count))
        Next
        datafile.Close()
        WebBrowser1.Navigate(Application.StartupPath & "\character-create.html")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        clear()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        AboutBox1.Show()
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Application.Exit()
    End Sub

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        AboutBox1.Show()
    End Sub

    Private Sub LicenseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LicenseToolStripMenuItem.Click
        Form2.Show()
    End Sub

    Private Sub ThanksToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ThanksToolStripMenuItem.Click
        Form3.Show()
    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Dim oReader As StreamReader
        load_sheet = False
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            oReader = New StreamReader(OpenFileDialog1.FileName)
            file_char_name = Trim(oReader.ReadLine)
            file_char_region = Trim(oReader.ReadLine)
            file_char_realm = Trim(oReader.ReadLine)
            oReader.Close()
            clear()
            reg_click = file_char_region
            load_sheet = True
            If file_char_region = 0 Then
                comboEU.Visible = True
                comboUSA.Visible = False
                comboEU.SelectedIndex = file_char_realm
                char_realm = Trim(comboEU.SelectedItem)
            ElseIf file_char_region = 1 Then
                comboEU.Visible = False
                comboUSA.Visible = True
                comboUSA.SelectedIndex = file_char_realm
                char_realm = Trim(comboUSA.SelectedItem)
            End If
            get_character_information()
        End If
    End Sub

    Private Sub SaveFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFileToolStripMenuItem.Click
        Dim oWriteFile As StreamWriter
        If Trim(txtName.Text) = "" Then
            txtName.Focus()
            Exit Sub
        End If
        file_char_name = Trim(txtName.Text)
        file_char_region = reg_click
        If comboRegion.SelectedIndex = 0 Then
            file_char_realm = comboEU.SelectedIndex
        End If
        If comboRegion.SelectedIndex = 1 Then
            file_char_realm = comboUSA.SelectedIndex
        End If
        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            oWriteFile = New StreamWriter(SaveFileDialog1.FileName, True)
            oWriteFile.WriteLine(Trim(file_char_name))
            oWriteFile.WriteLine(Trim(file_char_region))
            oWriteFile.WriteLine(Trim(file_char_realm))
            oWriteFile.Close()
        End If
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = False Then
            Me.Width = 859
            Me.Height = 593
            Me.Top = 201
            Me.Left = 210
        ElseIf CheckBox1.Checked = True Then
            Me.Width = 1236
            Me.Height = 694
            Me.Top = 201
            Me.Left = 28
        End If
    End Sub
End Class
